package com.smartstate.IOC.schedul;


import com.smartstate.IOC.config.BeanContext;
import com.smartstate.IOC.vo.DataVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

@Slf4j
public class DataRealTimeRunnableChild implements Callable<Map<String, Object>> {
    private DataVO dataVO;

    private String userId;

    public RedisTemplate redisTemplate;

    public SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    DataRealTimeRunnableChild(DataVO dataVO, String userId){
        redisTemplate = (RedisTemplate) BeanContext.getApplicationContext().getBean("redisTemplate");
        this.dataVO = dataVO;
        this.userId = userId;
    }

    @SneakyThrows
    @Override
    public Map<String, Object> call() {
        Map<String, Object> mapAttribute = new HashMap<>();
        String attributeValue = (String) redisTemplate.opsForHash().get(dataVO.getId(), dataVO.getAttribute());
        attributeValue = attributeValue.replace("\"", "");
        dataVO.setCurrentValue(attributeValue);
        dataVO.setTime(simpleDateFormat.format(new Date()));
        mapAttribute.put(dataVO.getId() + "." + dataVO.getAttribute(),dataVO);
        return mapAttribute;
    }
}
